﻿using System.Windows;
using System.Windows.Input;
using SABnzbdNET.Commands;
using SABnzbdNET.Events;
using SABnzbdNET.ServiceAgents;
using SABnzbdNET.Views;

namespace SABnzbdNET.ViewModels
{
    public class ToolbarViewModel : BaseViewModel
    {
        #region Fields and properties

        private readonly IEventAggregator eventAggregator;
        private DelegateCommand refreshInfoCommand;
        private DelegateCommand openAddTaskCommand;
        private DelegateCommand startServerCommand;
        private DelegateCommand pauseServerCommand;
        private DelegateCommand shutdownServerCommand;
        private DelegateCommand openSettingsCommand;
        private DelegateCommand openAboutCommand;

        public ICommand RefreshInfoCommand
        {
            get { return refreshInfoCommand ?? (refreshInfoCommand = new DelegateCommand(Update)); }
        }
        public ICommand OpenAddTaskCommand
        {
            get { return openAddTaskCommand ?? (openAddTaskCommand = new DelegateCommand(OpenAddTaskDialog)); }
        }
        public ICommand StartServerCommand
        {
            get { return startServerCommand ?? (startServerCommand = new DelegateCommand(StartServer)); }
        }
        public ICommand PauseServerCommand
        {
            get { return pauseServerCommand ?? (pauseServerCommand = new DelegateCommand(PauseServer)); }
        }
        public ICommand ShutdownServerCommand
        {
            get { return shutdownServerCommand ?? (shutdownServerCommand = new DelegateCommand(ShutDownServer)); }
        }
        public ICommand OpenSettingsCommand
        {
            get { return openSettingsCommand ?? (openSettingsCommand = new DelegateCommand(OpenSettingsDialog)); }
        }
        public ICommand OpenAboutCommand
        {
            get { return openAboutCommand ?? (openAboutCommand = new DelegateCommand(OpenAboutDialog)); }
        }

        #endregion

        public ToolbarViewModel()
        {
            eventAggregator = EventAggregatorProvider.Instance;
        }

        private void Update()
        {
            eventAggregator.Publish(new UpdateServerMessage());
        }

        private void UpdateServerInfo()
        {
            eventAggregator.Publish(new UpdateServerInfoMessage());
        }

        private void SetStatusMessage()
        {
            eventAggregator.Publish(new UpdateStatusInfoMessage
                                      {
                                          StatusMessage = "Updating",
                                          NotifyIconMessage = "Updating"
                                      });
        }

        private static void OpenAddTaskDialog()
        {
            var add = new AddTaskView { Owner = Application.Current.MainWindow };
            add.ShowDialog();
        }

        private void StartServer()
        {
            SetStatusMessage();
            new ServerAgent().StartServer(UpdateServerInfo, ShowError);
        }

        private void PauseServer()
        {
            SetStatusMessage();
            new ServerAgent().PauseServer(UpdateServerInfo, ShowError);
        }

        private void ShutDownServer()
        {
            MessageBoxResult result = MessageBox.Show("Are you sure you want to shut down SABnzbd?",
                "Shut down SABnzbd", MessageBoxButton.YesNo);
            if (result == MessageBoxResult.Yes)
            {
                SetStatusMessage();
                new ServerAgent().ShutDownServer(UpdateServerInfo, ShowError);
            }
        }

        private static void OpenSettingsDialog()
        {
            var set = new SettingsView { Owner = Application.Current.MainWindow };
            set.ShowDialog();
        }

        private static void OpenAboutDialog()
        {
            var about = new AboutView { Owner = Application.Current.MainWindow };
            about.ShowDialog();
        }

        private static void ShowError(string errorMessage)
        {
            MessageBox.Show(errorMessage, "Error");
        }
    }
}
